package com.android.imagerestoration;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.android.imagerestoration.units.FloatWindowsService;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity{

    private static final String TAG = "MainActivity";
    public static final int REQUEST_MEDIA_PROJECTION = 18;

    private ImageView mPreviewImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestCapturePermission();
        checkPermission();
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();

        Bitmap screenCaptureBitmap = ((App) getApplication()).getScreenCaptureBitmap();
        Log.d(TAG, "onBitmapDataEvent--screenCaptureBitmap->" + screenCaptureBitmap);

        if (screenCaptureBitmap != null) {
            mPreviewImageView.setImageBitmap(screenCaptureBitmap);
        }
    }

    private void initView() {
        mPreviewImageView = (ImageView) findViewById(R.id.img_content);
    }

    public void requestCapturePermission() {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            //5.0 之后才允许使用屏幕截图
            return;
        }

        MediaProjectionManager mediaProjectionManager = (MediaProjectionManager)
                getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        startActivityForResult(
                mediaProjectionManager.createScreenCaptureIntent(),
                REQUEST_MEDIA_PROJECTION);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.d(TAG, "requestCode---->" + requestCode);
        switch (requestCode) {
            case REQUEST_MEDIA_PROJECTION:

                Log.d(TAG, "resultCode---->" + resultCode);
                Log.d(TAG, "data---->" + data);
                if (resultCode == RESULT_OK && data != null) {
                    FloatWindowsService.setResultData(data);
                    startService(new Intent(getApplicationContext(), FloatWindowsService.class));
                }
                break;
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void checkPermission() {
        //所有的权限列表
        List<String> permissions = new ArrayList<>();
        permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        permissions.add(Manifest.permission.SYSTEM_ALERT_WINDOW);
        permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        permissions.add(Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
        permissions.add(Manifest.permission.SYSTEM_ALERT_WINDOW);

        //未获取的权限列表
        List<String> notGrantedList = new ArrayList<>();
        for (int i = 0; i < permissions.size(); i++) {
            String permission = permissions.get(i);
            int ret = ContextCompat.checkSelfPermission(this, permission);
            if (ret != PackageManager.PERMISSION_GRANTED) {
                notGrantedList.add(permission);
            }
        }

        //申请未获取的权限
        int notGrantedCount = notGrantedList.size();
        if (notGrantedCount > 0) {
            String[] permissionArr = notGrantedList.toArray(new String[notGrantedCount]);
            ActivityCompat.requestPermissions(this, permissionArr, 0);
        }

        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            this.startActivity(intent);
        }
    }
}